package com.android.hq.ganktoutiao.ui.adapter;

import android.app.Fragment;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;

import com.android.hq.ganktoutiao.R;
import com.android.hq.ganktoutiao.data.GanAnShunItem;
import com.android.hq.ganktoutiao.data.GanDaShuJuItem;
import com.android.hq.ganktoutiao.data.GanDaTuItem;
import com.android.hq.ganktoutiao.data.GanLvYouItem;
import com.android.hq.ganktoutiao.data.GanNewsItem;
import com.android.hq.ganktoutiao.data.GanShiTuItem;
import com.android.hq.ganktoutiao.data.GanTianXiaItem;
import com.android.hq.ganktoutiao.data.GanYunGuiZhouItem;
import com.android.hq.ganktoutiao.data.GanZhiHuiItem;
import com.android.hq.ganktoutiao.data.GankContentItem;
import com.android.hq.ganktoutiao.data.GankDetailData;
import com.android.hq.ganktoutiao.data.GankFooterItem;
import com.android.hq.ganktoutiao.data.GankHeaderItem;
import com.android.hq.ganktoutiao.data.GankImageItem;
import com.android.hq.ganktoutiao.data.GankItem;
import com.android.hq.ganktoutiao.data.GankSearchItem;
import com.android.hq.ganktoutiao.data.GankType;
import com.android.hq.ganktoutiao.data.HistoryFavItem;
import com.android.hq.ganktoutiao.data.JingJiItem;
import com.android.hq.ganktoutiao.data.NewsSildeItem;
import com.android.hq.ganktoutiao.utils.AppUtils;
import com.bumptech.glide.Glide;
import com.kcode.autoscrollviewpager.view.AutoViewPager;
import com.kcode.autoscrollviewpager.view.BaseViewPagerAdapter;
import com.mikepenz.fontawesome_typeface_library.FontAwesome;
import com.mikepenz.material_design_iconic_typeface_library.MaterialDesignIconic;

import java.util.List;

/**
 * Created by heqiang on 16-9-26.
 */
public class ListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private static final int TYPE_INVALID = -1;
    private static final int TYPE_CONTENT = 1;
    private static final int TYPE_IMAGE = 2;
    private static final int TYPE_HEADER = 3;
    private static final int TYPE_FOOTER = 4;
    private static final int TYPE_SEARCH_ITEM = 5;
    private static final int TYPE_HISTORY_FAV_ITEM = 6;
    private static final int TYPE_NEWS = 7;
    private static final int TYPEJINGJI = 8;
    private static final int TYPE_ZHIHUI = 9;
    private static final int TYPE_TIANXIA = 10;
    private static final int TYPE_DASHUJU = 11;
    private static final int TYPE_SHIJIE = 12;
    private static final int TYPE_YUNGUIZHOU = 13;
    private static final int TYPE_LVYOU = 14;
    private static final int TYPE_ANSHUN = 15;
    private static final int TYPE_SLIDE = 16;
    private static final int TYPE_SLIDE2 = 17;


    private Fragment mFragment;
    private List<GankItem> mList;

    public ListAdapter(Fragment fragment) {
        mFragment = fragment;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType) {
            case TYPE_CONTENT:
                return new ViewHolder.ContentViewHolder(parent);
            case TYPE_IMAGE:
                return new ViewHolder.ImageViewHolder(parent);
            case TYPE_HEADER:
                return new ViewHolder.HeaderViewHolder(parent);
            case TYPE_FOOTER:
                return new ViewHolder.FooterHolder(parent);
            case TYPE_SEARCH_ITEM:
                return new ViewHolder.SearchViewHolder(parent);
            case TYPE_HISTORY_FAV_ITEM:
                return new ViewHolder.HistoryFavViewHolder(parent);
            case TYPE_NEWS:
                return new ViewHolder.NewsViewHolder(parent);
            case TYPEJINGJI:
                return new ViewHolder.JingJiViewHolder(parent);
            case TYPE_ZHIHUI:
                return new ViewHolder.ZhiHuiViewHolder(parent);
            case TYPE_TIANXIA:
                return new ViewHolder.TianXiaViewHolder(parent);
            case TYPE_DASHUJU:
                return new ViewHolder.DaShuJuViewHolder(parent);
            case TYPE_SHIJIE:
                return new ViewHolder.ShiTuViewHolder(parent);
            case TYPE_YUNGUIZHOU:
                return new ViewHolder.YunGuiViewHolder(parent);
            case TYPE_LVYOU:
                return new ViewHolder.LvYouViewHolder(parent);
            case TYPE_ANSHUN:
                return new ViewHolder.AnShunViewHolder(parent);
            case TYPE_SLIDE:
                return new ViewHolder.SlideViewHolder(parent);

        }
        return null;
    }

    @Override
    public int getItemViewType(int position) {
        GankItem item = mList.get(position);
        if (item instanceof GankContentItem) {
            return TYPE_CONTENT;
        } else if (item instanceof GankImageItem) {
            return TYPE_IMAGE;
        } else if (item instanceof GankHeaderItem) {
            return TYPE_HEADER;
        } else if (item instanceof GankFooterItem) {
            return TYPE_FOOTER;
        } else if (item instanceof GankSearchItem) {
            return TYPE_SEARCH_ITEM;
        } else if (item instanceof HistoryFavItem) {
            return TYPE_HISTORY_FAV_ITEM;
        } else if (item instanceof GanNewsItem) {
            return TYPE_NEWS;
        } else if (item instanceof JingJiItem) {
            return TYPEJINGJI;
        } else if (item instanceof GanZhiHuiItem) {
            return TYPE_ZHIHUI;
        } else if (item instanceof GanTianXiaItem) {
            return TYPE_TIANXIA;
        } else if (item instanceof GanDaShuJuItem) {
            return TYPE_DASHUJU;
        } else if (item instanceof GanDaTuItem) {
            return TYPE_SHIJIE;
        } else if (item instanceof GanYunGuiZhouItem) {
            return TYPE_YUNGUIZHOU;
        } else if (item instanceof GanLvYouItem) {
            return TYPE_LVYOU;
        } else if (item instanceof GanDaTuItem) {
            return TYPE_ANSHUN;
        } else if (item instanceof NewsSildeItem) {
            return TYPE_SLIDE;
        }
        return TYPE_INVALID;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ViewHolder.ContentViewHolder) {
            ViewHolder.ContentViewHolder contentViewHolder = (ViewHolder.ContentViewHolder) holder;
            final GankContentItem item = (GankContentItem) mList.get(position);
            contentViewHolder.mTitle.setText(item.desc);
            String who = item.who;
            if (!TextUtils.isEmpty(who))
                contentViewHolder.mFrom.setText(who);
            String time = AppUtils.formatPublishedTime(item.publishedAt);
            if (!TextUtils.isEmpty(time))
                contentViewHolder.mTime.setText(time);

            contentViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    AppUtils.startArticleDetailActivity(mFragment.getActivity(),
                            new GankDetailData(item._id, item.type, item.url, item.who, item.desc, item.publishedAt, System.currentTimeMillis()));
                }
            });
        } else if (holder instanceof ViewHolder.ImageViewHolder) {
            ViewHolder.ImageViewHolder imageViewHolder = (ViewHolder.ImageViewHolder) holder;
            Glide.with(mFragment)
                    .load(((GankImageItem) mList.get(position)).imageUrl)
                    .centerCrop()
                    .into(imageViewHolder.mImageView);
        } else if (holder instanceof ViewHolder.HeaderViewHolder) {
            ViewHolder.HeaderViewHolder headerViewHolder = (ViewHolder.HeaderViewHolder) holder;
            String title = ((GankHeaderItem) mList.get(position)).title;
            headerViewHolder.mTitle.setText(title);
            switch (title) {
                case GankType.TYPE_BENEFIT:
                    AppUtils.setTextViewLeftDrawableForHeader(headerViewHolder.mTitle, MaterialDesignIconic.Icon.gmi_mood);
                    break;
                case GankType.TYPE_ANDROID:
                    AppUtils.setTextViewLeftDrawableForHeader(headerViewHolder.mTitle, MaterialDesignIconic.Icon.gmi_android);
                    break;
                case GankType.TYPE_IOS:
                    AppUtils.setTextViewLeftDrawableForHeader(headerViewHolder.mTitle, MaterialDesignIconic.Icon.gmi_apple);
                    break;
                case GankType.TYPE_APP:
                    AppUtils.setTextViewLeftDrawableForHeader(headerViewHolder.mTitle, MaterialDesignIconic.Icon.gmi_apps);
                    break;
                case GankType.TYPE_REST_VIDEO:
                    AppUtils.setTextViewLeftDrawableForHeader(headerViewHolder.mTitle, MaterialDesignIconic.Icon.gmi_collection_video);
                    break;
                case GankType.TYPE_EXPAND_RES:
                    AppUtils.setTextViewLeftDrawableForHeader(headerViewHolder.mTitle, FontAwesome.Icon.faw_location_arrow);
                    break;
                default:
                    AppUtils.setTextViewLeftDrawableForHeader(headerViewHolder.mTitle, MaterialDesignIconic.Icon.gmi_more);
                    break;
            }
        } else if (holder instanceof ViewHolder.SearchViewHolder) {
            ViewHolder.SearchViewHolder contentViewHolder = (ViewHolder.SearchViewHolder) holder;
            final GankSearchItem item = (GankSearchItem) mList.get(position);
            contentViewHolder.mTitle.setText(item.desc);
            String who = item.who;
            if (!TextUtils.isEmpty(who))
                contentViewHolder.mFrom.setText(who);
            String time = AppUtils.formatPublishedTime(item.publishedAt);
            if (!TextUtils.isEmpty(time))
                contentViewHolder.mTime.setText(time);
            contentViewHolder.mType.setText(item.type);

            contentViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    AppUtils.startArticleDetailActivity(mFragment.getActivity(),
                            new GankDetailData(item.ganhuo_id, item.type, item.url, item.who, item.desc, item.publishedAt, System.currentTimeMillis()));
                }
            });
        } else if (holder instanceof ViewHolder.HistoryFavViewHolder) {
            ViewHolder.HistoryFavViewHolder contentViewHolder = (ViewHolder.HistoryFavViewHolder) holder;
            final HistoryFavItem item = (HistoryFavItem) mList.get(position);
            contentViewHolder.mTitle.setText(item.title);
            String who = item.who;
            if (!TextUtils.isEmpty(who))
                contentViewHolder.mFrom.setText(who);
            String time = AppUtils.formatPublishedTime(item.published_date);
            if (!TextUtils.isEmpty(time))
                contentViewHolder.mTime.setText(time);
            contentViewHolder.mType.setText(item.gank_type);

            contentViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    AppUtils.startArticleDetailActivity(mFragment.getActivity(),
                            new GankDetailData(item.gank_id, item.gank_type, item.url, item.who, item.title, item.published_date, System.currentTimeMillis()));
                }
            });

            //首页
        } else if (holder instanceof ViewHolder.NewsViewHolder) {
            ViewHolder.NewsViewHolder contentViewHolder = (ViewHolder.NewsViewHolder) holder;
            final GanNewsItem item = (GanNewsItem) mList.get(position);
            contentViewHolder.mText.setText(item.main_title);
            contentViewHolder.mTime.setText(item.publish_date);
            contentViewHolder.mYuDu.setText(item.read_count + "");
            String img = "http://appufile.m.gzrbs.com.cn/ufile/imgsrv/resize/" + item.list_image;
            final String url = "http://jgz.gzrbs.com.cn/home/article/" + item.id;

            Glide.with(mFragment)
                    .load(img)
                    .centerCrop()
                    .into(contentViewHolder.mImg);

            contentViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    AppUtils.startArticleDetailActivity(mFragment.getActivity(),
                            new GankDetailData(item.story_abstract, item.story_type, url, item.story_tag, item.main_title, item.read_count, System.currentTimeMillis()));
                }
            });

            //推荐
        } else if (holder instanceof ViewHolder.JingJiViewHolder) {
            ViewHolder.JingJiViewHolder contentViewHolder = (ViewHolder.JingJiViewHolder) holder;
            final JingJiItem item = (JingJiItem) mList.get(position);
            contentViewHolder.jText.setText(item.main_title);
            contentViewHolder.jTime.setText(item.publish_date);
            contentViewHolder.jYuDu.setText(item.read_count + "");
            String img = "http://appufile.m.gzrbs.com.cn/ufile/imgsrv/resize/" + item.list_image;
            final String url = "http://jgz.gzrbs.com.cn/home/article/" + item.id;

            Glide.with(mFragment)
                    .load(img)
                    .centerCrop()
                    .into(contentViewHolder.jImg);

            contentViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    AppUtils.startArticleDetailActivity(mFragment.getActivity(),
                            new GankDetailData(item.story_abstract, item.story_type, url, item.story_tag, item.main_title, item.read_count, System.currentTimeMillis()));
                }
            });

            //智汇
        } else if (holder instanceof ViewHolder.ZhiHuiViewHolder) {
            ViewHolder.ZhiHuiViewHolder contentViewHolder = (ViewHolder.ZhiHuiViewHolder) holder;
            final GanZhiHuiItem item = (GanZhiHuiItem) mList.get(position);
            contentViewHolder.zText.setText(item.main_title);
            contentViewHolder.zTime.setText(item.publish_date);
            contentViewHolder.zYuDu.setText(item.read_count + "");
            String img = "http://appufile.m.gzrbs.com.cn/ufile/imgsrv/resize/" + item.list_image;
            final String url = "http://jgz.gzrbs.com.cn/home/article/" + item.id;

            Glide.with(mFragment)
                    .load(img)
                    .centerCrop()
                    .into(contentViewHolder.zImg);

            contentViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    AppUtils.startArticleDetailActivity(mFragment.getActivity(),
                            new GankDetailData(item.story_abstract, item.story_type, url, item.story_tag, item.main_title, item.read_count, System.currentTimeMillis()));
                }
            });

            //天下
        } else if (holder instanceof ViewHolder.TianXiaViewHolder) {
            ViewHolder.TianXiaViewHolder contentViewHolder = (ViewHolder.TianXiaViewHolder) holder;
            final GanTianXiaItem item = (GanTianXiaItem) mList.get(position);
            contentViewHolder.tText.setText(item.main_title);
            contentViewHolder.tTime.setText(item.publish_date);
            contentViewHolder.tYuDu.setText(item.read_count + "");
            String img = "http://appufile.m.gzrbs.com.cn/ufile/imgsrv/resize/" + item.list_image;
            final String url = "http://jgz.gzrbs.com.cn/home/article/" + item.id;

            Glide.with(mFragment)
                    .load(img)
                    .centerCrop()
                    .into(contentViewHolder.tImg);

            contentViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    AppUtils.startArticleDetailActivity(mFragment.getActivity(),
                            new GankDetailData(item.story_abstract, item.story_type, url, item.story_tag, item.main_title, item.read_count, System.currentTimeMillis()));
                }
            });

            //大数据
        } else if (holder instanceof ViewHolder.DaShuJuViewHolder) {
            ViewHolder.DaShuJuViewHolder contentViewHolder = (ViewHolder.DaShuJuViewHolder) holder;
            final GanDaShuJuItem item = (GanDaShuJuItem) mList.get(position);
            contentViewHolder.dText.setText(item.main_title);
            contentViewHolder.dTime.setText(item.publish_date);
            contentViewHolder.dYuDu.setText(item.read_count + "");
            String img = "http://appufile.m.gzrbs.com.cn/ufile/imgsrv/resize/" + item.list_image;
            final String url = "http://jgz.gzrbs.com.cn/home/article/" + item.id;

            Glide.with(mFragment)
                    .load(img)
                    .centerCrop()
                    .into(contentViewHolder.dImg);

            contentViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    AppUtils.startArticleDetailActivity(mFragment.getActivity(),
                            new GankDetailData(item.story_abstract, item.story_type, url, item.story_tag, item.main_title, item.read_count, System.currentTimeMillis()));
                }
            });

            //视界
        } else if (holder instanceof ViewHolder.ShiTuViewHolder) {
            ViewHolder.ShiTuViewHolder contentViewHolder = (ViewHolder.ShiTuViewHolder) holder;
            final GanDaTuItem item = (GanDaTuItem) mList.get(position);
            contentViewHolder.sText.setText(item.main_title);
            contentViewHolder.sTime.setText(item.publish_date);
            contentViewHolder.sYuDu.setText(item.read_count + "");
            String img = "http://appufile.m.gzrbs.com.cn/ufile/imgsrv/resize/" + item.list_image;
            final String url = "http://jgz.gzrbs.com.cn/home/article/" + item.id;

            Glide.with(mFragment)
                    .load(img)
                    .centerCrop()
                    .into(contentViewHolder.sImg);

            contentViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    AppUtils.startArticleDetailActivity(mFragment.getActivity(),
                            new GankDetailData(item.story_abstract, item.story_type, url, item.story_tag, item.main_title, item.read_count, System.currentTimeMillis()));
                }
            });

            //云贵州
        } else if (holder instanceof ViewHolder.YunGuiViewHolder) {
            ViewHolder.YunGuiViewHolder contentViewHolder = (ViewHolder.YunGuiViewHolder) holder;
            final GanYunGuiZhouItem item = (GanYunGuiZhouItem) mList.get(position);
            contentViewHolder.yText.setText(item.main_title);
            contentViewHolder.yTime.setText(item.publish_date);
            contentViewHolder.yYuDu.setText(item.read_count + "");
            String img = "http://appufile.m.gzrbs.com.cn/ufile/imgsrv/resize/" + item.list_image;
            final String url = "http://jgz.gzrbs.com.cn/home/article/" + item.id;

            Glide.with(mFragment)
                    .load(img)
                    .centerCrop()
                    .into(contentViewHolder.yImg);

            contentViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    AppUtils.startArticleDetailActivity(mFragment.getActivity(),
                            new GankDetailData(item.story_abstract, item.story_type, url, item.story_tag, item.main_title, item.read_count, System.currentTimeMillis()));
                }
            });

            //旅游
        } else if (holder instanceof ViewHolder.LvYouViewHolder) {
            ViewHolder.LvYouViewHolder contentViewHolder = (ViewHolder.LvYouViewHolder) holder;
            final GanLvYouItem item = (GanLvYouItem) mList.get(position);
            contentViewHolder.yText.setText(item.main_title);
            contentViewHolder.yTime.setText(item.publish_date);
            contentViewHolder.yYuDu.setText(item.read_count + "");
            String img = "http://appufile.m.gzrbs.com.cn/ufile/imgsrv/resize/" + item.list_image;
            final String url = "http://jgz.gzrbs.com.cn/home/article/" + item.id;

            Glide.with(mFragment)
                    .load(img)
                    .centerCrop()
                    .into(contentViewHolder.yImg);

            contentViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    AppUtils.startArticleDetailActivity(mFragment.getActivity(),
                            new GankDetailData(item.story_abstract, item.story_type, url, item.story_tag, item.main_title, item.read_count, System.currentTimeMillis()));
                }
            });

            //安顺
        } else if (holder instanceof ViewHolder.AnShunViewHolder) {
            ViewHolder.AnShunViewHolder contentViewHolder = (ViewHolder.AnShunViewHolder) holder;
            final GanDaTuItem item = (GanDaTuItem) mList.get(position);
            contentViewHolder.yText.setText(item.main_title);
            contentViewHolder.yTime.setText(item.publish_date);
            contentViewHolder.yYuDu.setText(item.read_count + "");
            String img = "http://appufile.m.gzrbs.com.cn/ufile/imgsrv/resize/" + item.list_image;
            final String url = "http://jgz.gzrbs.com.cn/home/article/" + item.id;

            Glide.with(mFragment)
                    .load(img)
                    .centerCrop()
                    .into(contentViewHolder.yImg);

            contentViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    AppUtils.startArticleDetailActivity(mFragment.getActivity(),
                            new GankDetailData(item.story_abstract, item.story_type, url, item.story_tag, item.main_title, item.read_count, System.currentTimeMillis()));
                }
            });

            //轮播图
        } else if (holder instanceof ViewHolder.SlideViewHolder) {
            ViewHolder.SlideViewHolder slideViewHolder = (ViewHolder.SlideViewHolder) holder;
            NewsSildeItem sildeItem = (NewsSildeItem) mList.get(position);
            AutoViewPager autoViewPager = new AutoViewPager(mFragment.getActivity().getApplication());
            BaseViewPagerAdapter adapter = new BaseViewPagerAdapter<GanNewsItem>(mFragment.getActivity().getApplication(), listener) {
                @Override
                public void loadImage(ImageView imageView, int i, GanNewsItem t) {

                    //利用glide加载图片
                    Glide.with(mFragment).load(t.getImageUrl()).into(imageView);
                }
            };

            adapter.init(autoViewPager, adapter);
            for (GanNewsItem item : sildeItem.list) {
                adapter.add(item);
            }

            slideViewHolder.autoScrollViewpager.setAdapter(adapter);
        }
    }

    //添加监听器
    protected BaseViewPagerAdapter.OnAutoViewPagerItemClickListener listener = new BaseViewPagerAdapter.OnAutoViewPagerItemClickListener<GanNewsItem>() {
        @Override
        public void onItemClick(int i, GanNewsItem o) {
            AppUtils.startArticleDetailActivity(mFragment.getActivity(),
                    new GankDetailData(o.story_abstract, o.story_type, o.getNewsUrl(), o.story_tag, o.main_title, o.read_count, System.currentTimeMillis()));
        }
    };

    @Override
    public int getItemCount() {
        return mList != null ? mList.size() : 0;
    }

    public void updateData(List<GankItem> list){
        if(mList == null || !mList.containsAll(list)){
            forceUpdateData(list);
        }else{
            Toast.makeText(mFragment.getActivity(), R.string.text_update, Toast.LENGTH_SHORT).show();
        }
    }

    public void forceUpdateData(List<GankItem> list){
        mList = list;
        notifyDataSetChanged();
    }

    public void loadMoreData(List<GankItem> list){
        mList.remove(mList.size() - 1);
        notifyItemRangeRemoved(mList.size(),1);
        if(list != null) {
            int size = mList.size();
            mList.addAll(size,list);
            notifyItemRangeInserted(size,list.size());
        }
    }

    public void onLoadMore(){
        int size = mList.size();
        mList.add(new GankFooterItem());
        notifyItemRangeInserted(size,1);
    }
}
